import express from 'express';
import compression from 'compression';
import cors from 'cors';
import ejs from 'ejs';
import config from './config.js';
import extendExpressMiddleware from './helpers/express_middleware.js';
import { passportJwtLogin, authMiddleware } from './helpers/auth_middleware.js';
import AuthController from './controllers/auth.js';
import AccountController from './controllers/account.js';
import HomeController from './controllers/home.js';
import ComponentsDataController from './controllers/components_data.js';
import FileUploaderController from './controllers/fileuploader.js';
import S3UploaderController from './controllers/s3uploader.js';
import AuditsController from  './controllers/audits.js';
import BlocksController from  './controllers/blocks.js';
import CoursesController from  './controllers/courses.js';
import EnrollmentsController from  './controllers/enrollments.js';
import GradesController from  './controllers/grades.js';
import PermissionsController from  './controllers/permissions.js';
import RolesController from  './controllers/roles.js';
import SmartContractsController from  './controllers/smartcontracts.js';
import SmartContractTransactionsController from  './controllers/smartcontracttransactions.js';
import StudentgradesummaryController from  './controllers/studentgradesummary.js';
import UsersController from  './controllers/users.js';
import SystemPerformanceController from './controllers/system_performance.js';
import EmployerApplicationsController from './controllers/employerapplications.js';
import EmployerPublicController from './controllers/employer_public.js';
import ApiKeysController from './controllers/apikeys.js';
import GradeReportsController from './controllers/grade_reports.js';

//添加注释


const app = express();

//set view engine use to return Html
app.set('views', 'views');
app.engine('html', ejs.renderFile);
app.set('view engine', 'ejs');
// compress all responses
app.use(compression({ threshold: 0 }));
//allow cors on localhost
app.use(cors()); // disable when deploy to production
app.use(express.static(config.app.publicDir))
app.use(express.json()) // Parses json, multi-part (file), url-encoded
app.use(express.urlencoded({ extended:true, limit:'50mb' }));
extendExpressMiddleware(app);
app.use(passportJwtLogin);

// 系统性能API - 不需要认证
app.use('/api/system-performance', SystemPerformanceController);

// 企业公开申请API - 不需要认证
app.use('/api/employer', EmployerPublicController);
app.use('/api/grade-reports', GradeReportsController);

// 其它API路由需要认证
app.use('/api/', authMiddleware);

//bind page route to the controllers
app.use('/api/', HomeController);
app.use('/api/auth', AuthController);
app.use('/api/account', AccountController);
app.use('/api/audits', AuditsController);
app.use('/api/blocks', BlocksController);
app.use('/api/employerapplications', EmployerApplicationsController);
app.use('/api/apikeys', ApiKeysController);
app.use('/api/courses', CoursesController);
app.use('/api/enrollments', EnrollmentsController);
app.use('/api/grades', GradesController);
app.use('/api/permissions', PermissionsController);
app.use('/api/roles', RolesController);
app.use('/api/smartcontracts', SmartContractsController);
app.use('/api/smartcontracttransactions', SmartContractTransactionsController);
app.use('/api/studentgradesummary', StudentgradesummaryController);
app.use('/api/users', UsersController);
app.use('/api/components_data', ComponentsDataController);
app.use('/api/fileuploader', FileUploaderController);
app.use('/api/s3uploader', S3UploaderController);

app.get('*', function(req, res){
    res.status(404).json("Page not found");
});

let port = 8060;
//start app
app.listen(port, () => {
    console.log('Server is up and running on port: ' + port);
});